package consoleWeb;

import java.io.IOException;
import pasantias.Dominio.*;
import org.Console.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class WebPrinter extends HttpServlet {

	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		
		BoardBuilder board = getBoardBuilder(request);
		InmutableBoard inmutableBoard = board.build();
		
		Text text = new Text();
		inmutableBoard.draw(text);
		String map = text.printMap();

		request.setAttribute("map", map);
		request.getRequestDispatcher("/print.jsp").forward(request, response);
		
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		BoardBuilder board = getBoardBuilder(request);
			
		String figure = request.getParameter("figure");

		if (figure.equalsIgnoreCase("Square"))
		{
			int squareSize = Integer.parseInt(request.getParameter("squareSize"));
			int squarePositionX = Integer.parseInt(request.getParameter("squarePositionX"));
			int squarePositionY = Integer.parseInt(request.getParameter("squarePositionY"));
			 
			board.addFigure(new Square(new Dot(squarePositionX,squarePositionY),squareSize));
		}
		
		if (figure.equalsIgnoreCase("Rectangle"))
		{
			int rectangleWidth = Integer.parseInt(request.getParameter("rectangleWidth"));
			int rectangleHeight = Integer.parseInt(request.getParameter("rectangleHeight"));
			int rectanglePositionX = Integer.parseInt(request.getParameter("rectanglePositionX"));
			int rectanglePositionY = Integer.parseInt(request.getParameter("rectanglePositionY"));
			
			board.addFigure(new Rectangle(new Dot(rectanglePositionX,rectanglePositionY),rectangleWidth, rectangleHeight));
		}
		
		if (figure.equalsIgnoreCase("Circle"))
		{
			int circleRadius = Integer.parseInt(request.getParameter("circleRadius"));
			int circlePositionX = Integer.parseInt(request.getParameter("circlePositionX"));
			int circlePositionY = Integer.parseInt(request.getParameter("circlePositionY"));
			
			board.addFigure(new Circle(new Dot(circlePositionX,circlePositionY),circleRadius));
		}
		
		if (figure.equalsIgnoreCase("Dot"))
		{
			int dotPositionX = Integer.parseInt(request.getParameter("dotPositionX"));
			int dotPositionY = Integer.parseInt(request.getParameter("dotPositionY"));
			
			board.addFigure(new Dot(dotPositionX,dotPositionY));
		}
		
		request.getRequestDispatcher("/index.jsp").forward(request, response);
	
		/*TODO avisar que se agregó la figura
		 * faltan validaciones */	
		
	}

	private BoardBuilder getBoardBuilder(HttpServletRequest request) {

		BoardBuilder board = (BoardBuilder) request.getSession().getAttribute("board");

		if(board == null){
			board = new BoardBuilder(50,25);
			//Integer.parseInt(request.getParameter("boardWidth")), Integer.parseInt(request.getParameter("boardHeight"))
			request.getSession().setAttribute("board", board);
		}
		
		return board;
	}

}
